
<template>
  <div>
    <div style="margin-bottom: 10px;">
    <el-input v-model="cardId" placeholder="请输入会员卡ID..."  suffix-icon="el-icon-search" style="width: 200px"></el-input>
      <el-input v-model="memberId" placeholder="请输入会员名ID..."  suffix-icon="el-icon-search" style="width: 200px;margin-left: 5px"></el-input>
    <el-button style="margin-left: 5px;" @click="loadPostById">查询</el-button>
    <el-button @click="resetParam">重置</el-button>
      <el-button style="margin-left: 5px;" @click="add">新增</el-button>
  </div>


  <el-table :data="tableData"
              :header-cell-style="{background:'#eaecf3',color:'rgb(106,117,119)'}" border
    >

    <el-table-column prop="cardId" label="会员卡ID" width="80">
    </el-table-column>
    <el-table-column prop="memberId" label="会员ID" width="80">
    </el-table-column>
    <el-table-column prop="cardTypeId" label="卡类型ID" width="80">
    </el-table-column>
    <el-table-column prop="balance" label="余额" width="80">
    </el-table-column>
    <el-table-column prop="totalValue" label="总充值金额" width="100">
    </el-table-column>
    <el-table-column prop="issueDate" label="发卡日期" width="180">
    </el-table-column>
    <el-table-column prop="expiryDate" label="到期时间" width="180">
    </el-table-column>
    <el-table-column prop="status" label="卡状态" width="110">
    </el-table-column>
    <el-table-column prop="lastUsed" label="最后使用时间" width="180">
    </el-table-column>
    <el-table-column prop="operate" label="操作" width="260" >
      <template slot-scope="scope">
        <el-button @click="mod(scope.row)">编辑</el-button>
        <el-button @click="del(scope.row.cardId)">删除</el-button>
      </template>
    </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
    <el-dialog title="提示" :visible.sync="centerDialogVisible" width="40%" center>
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="会员ID" prop="memberId">
          <el-col :span="20">
            <el-input v-model="form.memberId"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="卡类型ID" prop="cardTypeId">
          <el-col :span="20">
            <el-input v-model="form.cardTypeId"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="余额" prop="balance">
          <el-col :span="20">
            <el-input v-model="form.balance"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="总充值金额" prop="totalValue">
        <el-col :span="20">
          <el-input v-model="form.totalValue"></el-input>
        </el-col>
      </el-form-item>
        <el-form-item label="到期日期" prop="expiryDate">
          <el-col :span="11">
            <el-date-picker type="date" placeholder="选择日期" v-model="form.expiryDate" style="width: 100%;"></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status" style="width: 100%;">
            <el-radio label="ACTIVE"></el-radio>
            <el-radio label="FROZEN"></el-radio>
            <el-radio label="EXPIRED"></el-radio>
            <el-radio label="LOST"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="最后使用日期" prop="lastUsed">
          <el-col :span="11">
            <el-date-picker type="date" placeholder="选择日期" v-model="form.lastUsed" style="width: 100%;"></el-date-picker>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: 'MemberCardManage',

  data() {
    return {
      tableData: [],
      pageSize:10,
      pageNum:1,
      total:0,
      cardId:'',
      centerDialogVisible: false,
      form: {
        cardId:'',
        memberId: '',
        cardTypeId: '',
        balance: '',
        totalValue: '',
        expiryDate: '',
        status: '',
        lastUsed: ''
      }
    }
  },
  methods:{
    resetForm() {
      this.$refs.form.resetFields();
    },
    mod(row){
      console.log(row)
      this.centerDialogVisible = true
      this.$nextTick(() => {
        this.form=row;
      });
    },
    add() {
      this.centerDialogVisible = true
      this.$nextTick(() => {
        this.resetForm();
      });
    },
    doSave() {
      this.$axios.post(this.$httpUrl + '/memberCard/save',this.form).then(res => res.data).then(res => {
        console.log(res)
        if (res.code == 200) {
          this.$message({
            message: '操作成功！',
            type: 'success'
          });
          this.loadPost();
          this.centerDialogVisible = false
        } else {
          this.$message({
            message: '操作失败！',
            type: 'error'
          })
        }
      });
    },
    doMod(){
      this.$axios.post(this.$httpUrl + '/memberCard/update',this.form).then(res => res.data).then(res => {
        console.log(res)
        if (res.code == 200) {
          this.$message({
            message: '操作成功！',
            type: 'success'
          });
          this.loadPost();
          this.centerDialogVisible = false
        } else {
          this.$message({
            message: '操作失败！',
            type: 'error'
          })
        }
      });
    },
    save(){
      this.$refs.form.validate((valid)=>{
        if(valid){
          if(this.form.cardId){
            this.doMod();
          }else {
            this.doSave();
          }
        }else {
          console.log('error submit');
          return false;
        }
      });
    },
    del(cardId) {
      console.log(cardId);

      this.$axios.get(this.$httpUrl + '/memberCard/delete?cardId=' + cardId)
          .then(res => res.data)
          .then(res => {
            console.log(res);
            if (res.code == 200) {
              this.$message({
                message: '操作成功！',
                type: 'success'
              });
              this.loadPost();
            } else {
              this.$message({
                message: '操作失败！',
                type: 'error'
              });
            }
          })
          .catch(error => {
            console.error('删除操作出错:', error);
            this.$message({
              message: '操作出错！',
              type: 'error'
            });
          });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum=1
      this.pageSize=val
      this.loadPost()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum=val
      this.loadPost()
    },
    loadGet(){
      this.$axios.get(this.$httpUrl+'/memberCard/list').then(res=>res
          .data).then(res=>{
        console.log(res)
      })
    },
    loadPost(){
      this.$axios.get(this.$httpUrl+'/memberCard/listPage',{
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize
        }
      }).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){
          this.tableData=res.data
          this.total=res.total
        }else {
          alert('获取数据失败')
        }

      })
    },
    loadPostById(){
      this.$axios.get(this.$httpUrl+'/memberCard/listPageById',{
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          cardId:this.cardId,
          memberId:this.memberId
        }
      }).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){
          this.tableData=res.data
          this.total=res.total
        }else {
          alert('获取数据失败')
        }

      })
    },
    resetParam() {
      this.cardId = '';
      this.memberId='';
      this.loadPost();
    }
  },
  beforeMount() {
    //this.loadGet();
    this.loadPost();
  }
}
</script>
<style scoped>

</style>